<?php

/**
 * @name PassportController
 * @desc 控制器
 */
class PassportController extends ApiBaseController
{
    /**
     * 默认动作
     */
    public function indexAction()
    {
        echo "Hello passport";
        return false;
    }

    public function LoginAction()
    {
        $params = $this->jsonData();
        $rule = [
            'username'	=> 'require',
            'password'	=> 'require',
            'verify'	=> 'require'
        ];
        $msg = [
            'moble.require' => '必须输入登录名或者手机号',
            'password.require' => '必须输入登录密码',
            'verify.require' => '必须输入验证码',
        ];
        $validate = new \Vendor\Kvalidate($rule,$msg);
        if(!$validate->check($params)){
            $this->ajaxReturn(400, $validate->getError());
        }

//        if(!\Vendor\Ksys::verify($params['verify'])){
//            $this->ajaxReturn(400, '验证码不正确');
//        }
        $adminModel = AdminModel::getSingleton();
        $adminInfo = $adminModel->find(['username|moble'=> $params['username']]);
        if(!$adminInfo){
            $this->ajaxReturn(400, '管理员不存在');
        }
        if($adminInfo['password'] != md5($params['password'])){
            $this->ajaxReturn(400, '密码错误');
        }
        if($adminInfo['status'] == 0){
            $this->ajaxReturn(400, '账户被禁止登录');
        }

        $tokenData = ['admin_id'=> $adminInfo['admin_id'], 'ctime'=> time()];
        //token
        $token = \Vendor\Kstring::getToken(json_encode($tokenData));

        $config = \Yaf\Application::app()->getConfig()->app;

        $permissions = AdminModel::$permissions[$adminInfo['admin_type']];

        $returnData = [
            'token'     => $token,
            'admin_id'  => $adminInfo['admin_id'],
            'username'	=> $adminInfo['username'],
            'moble'     => $adminInfo['moble'],
            'admin_type'    => $adminInfo['admin_type'],
            'permissions'   => $permissions
        ];
        $this->ajaxReturn(0, 'success', $returnData);
    }

    public function verifyAction() {
        $this->genVerificationCode();
        return false;
    }

    public function genVerificationCode()
    {
        $str = strval(rand(0, 9999));
        $strLen = strlen($str);
        $maxLen = 4;
        if ($diff = $maxLen - $strLen) {
            for ($i = 0; $i < $diff; $i++) {
                $str = '0' . $str;
            }
        }
        \Vendor\Ksys::verify("", "", $str);
    }

    //发送短信
    public function smsAction(){

        $params = $this->post();

        $rule = [
            'moble'     => 'require|regex:moble',
            'type' => 'require|in:reg,findpwd,findpwd_pay',
        ];
        $msg = [
            'moble.require' => '必须输入手机号',
            'moble.regex'   => '必须正确的手机号码',
            'type.require'  => '必须指定发送类型',
            'type.in'       => '错误的发送类型'
        ];
        $validate = new \Vendor\Kvalidate($rule,$msg);
        if(!$validate->check($params)){
            $this->ajaxReturn(400, $validate->getError());
        }

        $code = rand(1111, 9999);
        $sms = new \Vendor\ext\Sms();
        $res  = $sms->send($params['moble'], $code, $params['type'], $params['type']);
        if(!isset($res[1])){
            $this->ajaxReturn(400, '发送失败');
        }
        $this->ajaxReturn(0, '发送成功');
    }

}
